<?php

namespace common\models\survey;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%survey_attach}}".
 *
 * @property int $id
 * @property int $survey_id 排查ID
 * @property int $type 应用类型：0问题，1治理
 * @property int $user_id 用户ID
 * @property int $attach_id 附件ID
 * @property string $path 保存路径
 * @property int $format 文件格式
 * @property int $site_id 来源站点ID
 * @property int $channel_id 远端上传通道
 * @property int $status 附件状态
 * @property int $created_at
 * @property int $updated_at
 */
class SurveyAttach extends \common\db\ActiveRecord {

    /**
     * 状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%survey_attach}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['survey_id', 'type', 'user_id', 'attach_id', 'format', 'size', 'site_id', 'channel_id', 'status', 'created_at', 'updated_at'], 'integer'],
            [['path'], 'required'],
            [['path'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'survey_id' => '排查ID',
            'type' => '应用类型：0问题，1治理',
            'user_id' => '用户ID',
            'attach_id' => '附件ID',
            'path' => '保存路径',
            'format' => '文件格式',
            'size' => '文件大小（单位：字节）',
            'site_id' => '来源站点ID',
            'channel_id' => '远端上传通道',
            'status' => '附件状态',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getUrl() {
        return Url::attach($this->path);
    }

    /**
     * {@inheritdoc}
     */
    public function getThumb() {
        $path = $this->path;
        if ($path && Url::isRelative($path)) {
            $path .= '?x-oss-process=image/resize,m_fill,h_100,w_100';
        }
        return Url::attach($path);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'type' => function() {
                return $this->format ? 'video' : 'image';
            },
            'path',
            'url',
            'size',
            'status' => function() {
                return 'success';
            },
        ];
    }

}
